package capricornus.net.loader
{
	import capricornus.actor.res.ActionImage;
	import capricornus.actor.utils.ActionType;
	import capricornus.events.ResLoaderEvent;
	import capricornus.net.NetWorkManager;
	import capricornus.net.Path;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;

	public class ActionImageLoader extends ResLoader
	{
		private static const DIRECT_NAMES:Array = ["right","rightDown","down","leftDown","left","leftUp","up","rightUp"];
		
		private var _name:String;
		private var _type:int;
		
		private var _className:String;
		
		public function ActionImageLoader()
		{
			super();
		}
		
		public function loadImage( name:String, type:int ):void
		{
			_name = name;
			_type = type;
			_className = _name + "_" + ActionType.getName( type );
				
			var url:String = Path.getActorImagePath( name, type );
			load( url, NetWorkManager.instance.loaderContext );
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get type():int
		{
			return _type;
		}
		
		override protected function dispatchCompleteEvent(e:ResLoaderEvent):void
		{
			var mc:MovieClip = e.data as MovieClip;
			if( mc.hasOwnProperty( "OutputSet" ) )
			{
				var dict:Dictionary = mc["OutputSet"] as Dictionary;
				var ai:ActionImage = new ActionImage();
				
				var arrName:Array = DIRECT_NAMES;
				var i:int;
				var j:int;
				var arr:Array;
				for( i=0; i<arrName.length; i++ )
				{
					if( dict[_className+"_"+arrName[i]] != null )
					{
						arr = dict[_className+"_"+arrName[i]];
						ai.arrOffset.push( arr );
						ai.frames = arr.length;
					}
				}
				ai.angles = ai.arrOffset.length==1?1:8;
				
				var ad:ApplicationDomain = _loader.contentLoaderInfo.applicationDomain;
				var len:int;
				for( i=0; i<arrName.length; i++ )
				{
					var n:String = _className+"_"+arrName[i];
					arr = dict[n];
					var isMirror:Boolean = false;
					if( arr==null && ai.angles != 1 )
					{
						isMirror = true;
						n = _className + "_" + arrName[ActionImage.getOppositeDirect(i)];
						arr = dict[n];
					}
					if( arr==null )
					{
						continue;
						//throw new Error( "动作图不完整" );
					}
					len = arr.length;
					for( j=0; j<len; j++ )
					{
						var ne:String = n + "_" + j;
						if( ad.hasDefinition( ne ) )
						{
							var c:Class = ad.getDefinition( ne ) as Class;
							var bd:BitmapData = new c() as BitmapData;
							if( isMirror )
							{
								bd = getXScaleBitmapData( bd );
								var arrOff:Array = (ai.arrOffset[i][j] as Array);
								ai.arrOffset[ActionImage.getOppositeDirect(i)][j] = [Math.abs(arrOff[0]) - bd.width, arrOff[1]]; 
							}
							ai.arrImages.push( bd );
						}
						else
						{
							ai.arrImages.push( null );
						}
					}
				}
				e.data = ai;
			}
			
			super.dispatchCompleteEvent( e );
		}
		
		private function getXScaleBitmapData( bd:BitmapData ):BitmapData
		{
			var b:BitmapData = new BitmapData( bd.width, bd.height, true, 0 );
			b.draw( bd, new Matrix( -1 ) );
			return b;
		}
	}
}















